import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SealedObject;
import javax.crypto.spec.SecretKeySpec;

import palms.exceptions.InvalidClassMessage;
import palms.exceptions.InvalidTicketMessage;
import palms.exceptions.InvalidUserMessage;
import palms.messages.AuthRequest;
import palms.messages.AuthResponse;
import palms.messages.SIRequest;
import palms.messages.SIResponse;
import palms.messages.SIServiceInfo;
import palms.messages.SLRequest;
import palms.messages.SLServiceInfo;
import palms.messages.SLServiceResults;
import palms.messages.TGSClientInfo;
import palms.messages.TGSKeyChain;
import palms.messages.TGSRequest;
import palms.messages.TGSResponse;
import palms.security.PalmsSecurityManager;


public class testing {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		
			try {
				
				Socket s;
				String username = "fireman";
				String password = "inseguro";
				
				int KASport = 56421;
				int KTGSport = 56422;
				int SIport = 56423;
				
				//ENVIA 1
				AuthRequest authreq = new AuthRequest(username);
				s = new Socket("146.193.41.106", KASport);
				authreq.sendTroughSocket(s);
				
				//RECEBE 2
				
				AuthResponse ar = AuthResponse.receiveTroughSocket(s);
				s.close();
				
				SealedObject ticket_tgs = ar.getTgsTicket();
				SealedObject sealed_tgssessionkey = ar.getKey();
				
				SecretKeySpec tgssessionkey = (SecretKeySpec) PalmsSecurityManager.DesealObjectWithK(sealed_tgssessionkey, PalmsSecurityManager.getKeyFromString(password));
				
				//envia 3
				TGSClientInfo clientinfo = new TGSClientInfo(username, Calendar.getInstance().getTimeInMillis());
				SealedObject sealed_clientinfo = PalmsSecurityManager.SealObjectWithK(clientinfo, tgssessionkey);
				
				
				TGSRequest tgsrequest = new TGSRequest(ticket_tgs, sealed_clientinfo);
				
				s = new Socket("146.193.41.106", KTGSport);
				tgsrequest.sendTroughSocket(s);
				
				//recebe 4
				
				TGSResponse tgsresponse = TGSResponse.receiveTroughSocket(s);
				s.close();
				
				SealedObject ticket_si = tgsresponse.getTicketSI();
				SealedObject ticket_sl = tgsresponse.getTicketSL();
				SealedObject sealed_keychain = tgsresponse.getKeyChain();
				
				TGSKeyChain keychain = (TGSKeyChain) PalmsSecurityManager.DesealObjectWithK(sealed_keychain, tgssessionkey);
				
				SecretKeySpec sisessionkey = keychain.getClientSIkey();
				SecretKeySpec slsessionkey = keychain.getClientSLkey();
				
				//envia 5
				
				//Constroi pedido para o SL
				SLServiceInfo slsinfo = new SLServiceInfo(Calendar.getInstance().getTimeInMillis());
				SealedObject sealed_slsinfo = PalmsSecurityManager.SealObjectWithK(slsinfo, slsessionkey);
				
				SLRequest slreq = new SLRequest(ticket_sl, sealed_slsinfo);
				
				//Constroi pedido para o SI
				
				SIServiceInfo siinfo = new SIServiceInfo(username, Calendar.getInstance().getTimeInMillis(), slreq);
				SealedObject sealed_siinfo = PalmsSecurityManager.SealObjectWithK(siinfo, sisessionkey);
				
				SIRequest sireq = new SIRequest(ticket_si, sealed_siinfo);
				
				//envia pedido para o SI
				s = new Socket("146.193.41.106", SIport);
				sireq.sendTroughSocket(s);
				
				//recebe 8
				SIResponse siresp = SIResponse.receiveTroughSocket(s);
				s.close();
				
				SealedObject sealed_slresp = (SealedObject) siresp.getResult();
				SealedObject sealed_res =  (SealedObject) PalmsSecurityManager.DesealObjectWithK(sealed_slresp, sisessionkey);
				SLServiceResults res = (SLServiceResults) PalmsSecurityManager.DesealObjectWithK(sealed_res, slsessionkey);
				
				System.out.println(res.s);
				
				
				
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvalidClassMessage e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvalidUserMessage e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvalidTicketMessage e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvalidKeyException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalBlockSizeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchPaddingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (BadPaddingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			
			
			
				
			
			
		

	}

}
